package 刷题10月.leecodehot100;

import java.util.HashMap;
import java.util.Map;

public class Solution1 {
    public static int query (int[] arr, int times) {
        // write code here
        Map map=new HashMap<>();
        for(int i=0;i<arr.length;i++){
            if(!map.containsKey(arr[i])){
                map.put(arr[i],1);
            }
            else{
                map.put(arr[i],(int)map.get(arr[i])+1);
            }
        }

        for(int j=arr.length-1;j>=0;j--){
            if((int)map.get(arr[j])==times){
                return j-times+1;
            }
        }
        return -1;
    }





    //{1, 2, 2, 4, 4, 4, 6, 6, 7},2
    //{1, 2, 2, 4, 4, 4, 6, 6, 7},4
    public static void main(String[] args) {
        System.out.println(query(new int[]{1, 2, 2, 4, 4, 4, 6, 6, 7},2));
        System.out.println(query(new int[]{1, 2, 2, 4, 4, 4, 6, 6, 7},4));
    }
}
